package y2016.date0202.am.dom生成xml;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.List;


public class DomXML01 {
    /**
     * @param args
     * @throws Exception
     */
    @Test
    public void main() throws Exception {
        SAXReader reader = new SAXReader();
        File file = new File("book.xml");
        Document document = reader.read(file);
        Element root = document.getRootElement();
        List childElements = root.elements();

        for (Object item : childElements) {
            if (!(item instanceof Element)) {
                continue;
            }
            Element child = (Element) item;

            //未知属性名情况下
            //			List<Attribute> attributeList = child.attributes();
            //			for (Attribute attr : attributeList) {
            //				System.out.println(attr.getName() + ": " + attr.getValue());
            //			}

            //已知属性名情况下
            System.out.println("id: " + child.attributeValue("id"));

            //未知子元素名情况下
//			List<Element> elementList = child.elements();
//			for (Element ele : elementList) {
//				System.out.println(ele.getName() + ": " + ele.getText());
//			}
//			System.out.println();

            //已知子元素名的情况下
            System.out.println("title" + child.elementText("title"));
            System.out.println("author" + child.elementText("author"));
            //这行是为了格式化美观而存在
            System.out.println();
        }
    }
}


